<?
// 参数表: 产品编号/属性编号/数量 
// params: peoduct_id/var_id/qty 

// 购物车缺排序功能
function get_cart(){
	$res=array();
	if(isset($_COOKIE['cart'])&&!empty($_COOKIE['cart'])){
		$cookie_cart=$_COOKIE['cart'];
		$products=explode('&',$cookie_cart);
		foreach($products as $product){
			$temp=explode('|',$product);
			if(!empty($temp[0]) && !empty($temp[2]))
			$res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>$temp[2]);
		}
	}
	return $res;
}
function get_cart_qty(){
	$tem=get_cart();
	$qty=0;
	foreach ($tem as $val) { $qty+=$val['qty']; }
	return $qty;
}
function get_order_price($currency_rate){
	global $product,$product_variation;
	$tem=get_cart();
	$order_price=0;

	foreach ($tem as $val) {
		$tem_product=$product->get_row($val['pid']);
		$tem_price=round($tem_product['price']*$currency_rate,2);
		$tem_stock=$tem_product['stock'];
		$tem_qty=$val['qty'];

		// variation
		if($val['var']){
			$tem_var=$product_variation->get_row($val['var']);
			$tem_price=round($tem_var['price']*$currency_rate,2); // var
			$tem_stock=$tem_var['stock'];  // var
		}
		$has_stock=$tem_stock>=$tem_qty?1:0;

		if($has_stock){
			$tem_total=$tem_qty*$tem_price;
			$order_price+=$tem_total;
		}
	
	}
	return $order_price;
}
// 从购物车移除条目, 返回购物车内商品件数
function remove_from_cart($pid,$var){
	$now_qty=0;
	$res=array();
	if(isset($_COOKIE['cart']) && !empty($_COOKIE['cart'])){
		$cookie_cart=$_COOKIE['cart'];
		$products=explode('&',$cookie_cart);
		foreach($products as $key => $product){
			$temp=explode('|',$product);
			if($pid==$temp[0] && $var==$temp[1]) continue; 
			$res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>$temp[2]);
			$now_qty+=$temp[2]; // 计算购物车商品件数
		}
	}
	write_to_cart($res);
	return $now_qty;
}
// 添加条目到购物车, 返回购物车内商品件数
function add_to_cart($pid,$var,$qty){
	$now_qty=get_cart_qty();
	$sameSkuExist=false;
	$res=array();
	if(isset($_COOKIE['cart'])&&!empty($_COOKIE['cart'])){
		$cookie_cart=$_COOKIE['cart'];
		$products=explode('&',$cookie_cart);
		foreach($products as $product){
			$temp=explode('|',$product);
			if($pid==$temp[0] && $var==$temp[1] && $qty>0){
				$res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>($temp[2]+$qty));
				$sameSkuExist=true;
			}
			else $res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>$temp[2]);
		}
	}
	if($pid!=0 && !$sameSkuExist && $qty>0){
		$res[]=array('pid'=>$pid, 'var'=>$var, 'qty'=>$qty);
	}
	///print_r($res); exit;
	write_to_cart($res);
	$now_qty+=$qty;
	return $now_qty;
}
// 添加多个条目到购物车, 返回购物车内商品件数, 须在上游保证数据可用性
// add items to cart by array
function add_to_cart_by_array($pids,$vars,$qtys){
	$now_qty=get_cart_qty();
	$res=array();
	if(isset($_COOKIE['cart']) && !empty($_COOKIE['cart'])){
		$cookie_cart=$_COOKIE['cart'];
		$products=explode('&',$cookie_cart);
		$num_pids=count($pids);

		foreach($products as $product){
			$sameSkuExist=false;
			$temp=explode('|',$product);
			//pre($temp);
			for ($i=0;$i<$num_pids;$i++){
				$pid=$pids[$i];
				$var=$vars[$i];
				$qty=$qtys[$i];

				if($pid==$temp[0] && $var==$temp[1] && $qty>0){
					$res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>($temp[2]+$qty));
					$sameSkuExist=true;
					unset($pids[$i]);
					unset($vars[$i]);
					unset($qtys[$i]);
				}
			}
			if(! $sameSkuExist) {
				if(!empty($temp[0]) && !empty($temp[2]))
				$res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>$temp[2]);
			}
		}
	}
	sort($pids);
	sort($vars);
	sort($qtys);

	if(count($pids)){
		$num_pids=count($pids);
		for ($i=0;$i<$num_pids;$i++){
			$pid=$pids[$i];
			$var=$vars[$i];
			$qty=$qtys[$i];
			$res[]=array('pid'=>$pid,'var'=>$var,'qty'=>$qty);
		}
	}
	//print_r($res); exit;
	write_to_cart($res);
	$now_qty+=$qty;
	return $now_qty;
}
// 更新购物车中的商品数量
function cart_update($pid,$var,$qty){
	$res=array();
	if(isset($_COOKIE['cart']) && !empty($_COOKIE['cart'])){
		$cookie_cart=$_COOKIE['cart'];
		$products=explode('&',$cookie_cart);
		foreach($products as $product){
			$temp=explode('|',$product);
			if($pid==$temp[0] && $var==$temp[1] && $qty>0){
				continue;
			}
			$res[]=array('pid'=>$temp[0],'var'=>$temp[1],'qty'=>$temp[2]);
		}
	}
	if($pid!=0 && $qty>0){
		$res[]=array('pid'=>$pid, 'var'=>$var, 'qty'=>$qty);
	}
	write_to_cart($res);
	return count($res);
}

function write_to_cart($cart){
	$k=0;
	$cart_str='';
	$cart=quick_sort($cart,'pid');
	foreach($cart as $product){
		if($k)$cart_str.='&';
		$cart_str.=$product['pid'].'|'.$product['var'].'|'.$product['qty'];
		$k++;
	}
	setcookie("cart", $cart_str,time()+3600*24*30);
}

function cart_clear(){
	setcookie("cart", '', time()-3600);
}

// end of file